package cn.lcj.org.controller;

import cn.lcj.basic.annotation.PreAuthorize;
import cn.lcj.basic.utils.AjaxResult;
import cn.lcj.basic.utils.PageList;
import cn.lcj.org.domain.Department;
import cn.lcj.org.query.DepartmentQuery;
import cn.lcj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * $
 *
 * @return $
 * @date $ $
 * @description $
 */
@Api(value = "部门类接口",description = "处理部门的CRUD操作的")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询单个 用get  url路径传参
     * @PathVariable 获取路径参数  不加获取不了数据
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：查询单个")
    @GetMapping("/{id}")
    //自定义权限注解
    @PreAuthorize(name="查询单个部门",sn = "department:getOne")
    public Department findOne(@PathVariable Long id){
        return departmentService.loadById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @ApiOperation(value = "接口：查询全部")
    @GetMapping
    //http://localhost:8080/department
    public List <Department> findAll(){
        return  departmentService.loadAll();
    }

    /**
     * 返回值需要用AjaxResult接收
     * 删除
     * @PathVariable url上获取参数
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
             departmentService.del(id);
             return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 新增与修改
     * 返回值需要用AjaxResult接收 需要返回操作信息
     * 前端对象的接收需要用@RequestBody 从请求体里面获取数据
     * @param department
     * @return
     */
    @ApiOperation(value = "接口：新增与修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                 departmentService.save(department);
                 return new AjaxResult();
            }else {
                departmentService.update(department);
                return new AjaxResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }

        }

    /**
     * 分页查询
     * 用封装类的对象做参数来接收
     * 前端对象的接收需要用@RequestBody 从请求体里面获取数据
     * @param query
     * @return
     */
    @ApiOperation(value = "接口：分页查询")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
        }

    /**
     * 返回值需要用AjaxResult接收
     * 批量删除
     * @PathVariable url上获取参数
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    public AjaxResult patchList(@RequestBody List<Long> ids){
        try {
           departmentService.patchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     *
     * @return
     */
    @ApiOperation(value = "接口：无限级数的接口")
    @GetMapping("/deptTree")
    //http://localhost:8080/department
    public List <Department> departmentTree(){
        return  departmentService.departmentTree();
    }


}
